home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / pascal / swag / textfile.swg / 0035_Reading a Text File.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-02-03  |  6.1 KB  |  247 lines

  1. {
  2. After much trial and error, and finding some helpful code from the SWAG
  3. support team (thanks!) this is what I came up with.  It can handle text
  4. files up to 750,000 bytes and does basically what I'm looking for, but
  5. the scrolling isn't as smooth as it should be.  Also, the lines of
  6. text are limited to 79 characters...  (The source code can probably be
  7. streamlined a lot too, like I said, I'm fairly new at this...)
  8. }
  9.  
  10.  Program Reader;
  11.  
  12.  uses Crt, Dos;
  13.  
  14. {$R-,S- }
  15.  
  16. Procedure GetFileMode; Assembler;
  17.  
  18. Asm
  19.            CLC
  20.            CMP    ES:[DI].TextRec.Mode, fmInput
  21.            JE     @1
  22.            MOV    [InOutRes], 104         { 'File not opened For reading' }
  23.            xor    AX, AX                  { Zero out Function result }
  24.            xor    DX, DX
  25.            STC
  26. @1:
  27. end;  { GetFileMode }
  28.  
  29. Function TextFilePos(Var f : Text) : LongInt; Assembler;
  30.  
  31. Asm
  32.         LES    DI, f
  33.         CALL   GetFileMode
  34.         JC     @1
  35.  
  36.         xor    CX, CX                  { Get position of File Pointer }
  37.         xor    DX, DX
  38.         MOV    BX, ES:[DI].TextRec.handle
  39.         MOV    AX, 4201h
  40.         inT    21h                     { offset := offset-Bufend+BufPos }
  41.                                 xor    BX, BX
  42.         SUB    AX, ES:[DI].TextRec.Bufend
  43.         SBB    DX, BX
  44.         ADD    AX, ES:[DI].TextRec.BufPos
  45.         ADC    DX, BX
  46. @1:
  47. end;  { TextFilePos }
  48.  
  49.  
  50. Function TextFileSize(Var f : Text) : LongInt; Assembler;
  51.  
  52. Asm
  53.             LES    DI, f
  54.             CALL   GetFileMode
  55.             JC     @1
  56.  
  57.             xor    CX, CX                  { Get position of File Pointer }
  58.     xor    DX, DX
  59.     MOV    BX, ES:[DI].TextRec.handle
  60.     MOV    AX, 4201h
  61.             inT    21h
  62.     PUSH   DX                      { Save current offset on the stack }
  63.             PUSH   AX
  64.     xor    DX, DX                  { Move File Pointer to Eof }
  65.     MOV    AX, 4202h
  66.     inT    21h
  67.     POP    SI
  68.     POP    CX
  69.             PUSH   DX                      { Save Eof position }
  70.     PUSH   AX
  71.     MOV    DX, SI                  { Restore old offset }
  72.     MOV    AX, 4200h
  73.     inT    21h
  74.     POP    AX                      { Return result}
  75.     POP    DX
  76. @1:
  77. end;  { TextFileSize }
  78.  
  79. Procedure TextSeek(Var f : Text; n : LongInt); Assembler;
  80.  
  81. Asm
  82.     LES    DI, f
  83.           CALL   GetFileMode
  84.     JC     @2
  85.  
  86.     MOV    CX, Word Ptr n+2        { Move File Pointer }
  87.     MOV    DX, Word Ptr n
  88.     MOV    BX, ES:[DI].TextRec.Handle
  89.           MOV    AX, 4200h
  90.           inT    21h
  91.           JNC    @1                      { Carry flag = reading past Eof }
  92.           MOV    [InOutRes], AX
  93.           JMP    @2
  94.  
  95.                            { Force read next time }
  96. @1:  MOV    AX, ES:[DI].TextRec.Bufend
  97.                        MOV    ES:[DI].TextRec.BufPos, AX
  98. @2:
  99. end;  { TextSeek }
  100.     {end TextUtil }
  101.  
  102.  
  103.   Procedure HideCursor;  assembler;
  104.   asm
  105.     mov      ah,$01  { Function number }
  106.     mov      ch,$20
  107.     mov      cl,$00
  108.     Int      $10     { Call BIOS }
  109.   end;  { HideCursor }
  110.  
  111.  
  112.   Procedure RestoreCursor;  assembler;
  113.   asm
  114.     mov      ah,$01  { Function number }
  115.     mov      ch,$06  { Starting scan line }
  116.     mov      cl,$07  { Ending scan line }
  117.     int      $10     { Call BIOS }
  118.   end; { RestoreCursor }
  119.  
  120.  
  121.  Var
  122.      TxtFile : text;
  123.      s : string[79];
  124.      Cee : CHAR;
  125.  
  126.  Label RWLoop, Final, FileSizeError, WrongKey, NoParamError;
  127.  
  128.  Var
  129.     Size : Longint;
  130.     YY, GG, Counter : LongInt;
  131.     LineNumArray : Array[0..15000] Of LongInt;
  132.     MyText : Array[0..23] Of String[79];
  133.     InstructStr : String[79];
  134.     OrigColor, ColorSwitch : Integer;
  135.     LineNo : String[5];
  136.  Begin
  137.    OrigColor := TextAttr;
  138.    TextColor(11);
  139.    TextBackground(1);
  140.    InstructStr := 'Scroll (^) up - (v) down - (Page up/down) - (Home) - (End) - (ESC) Quit';
  141.    If ParamStr(1) = '' Then GoTo NoParamError;
  142.    Assign(TxtFile, ParamStr(1)); {'TEXTFILE.DOC';}
  143.    Reset(TxtFile);
  144.    Counter := -1;
  145.    ClrScr;
  146.    HideCursor;
  147.    If (TextFileSize(TxtFile)) >= 750000 Then GoTo FileSizeError;
  148.    While Not EOF(TxtFile) Do
  149.      Begin
  150.        Inc(Counter,1);
  151.        LineNumArray[Counter] := TextFilePos(TxtFile);
  152.        ReadLn(TxtFile);
  153.      End;
  154.    Inc(Counter,1);
  155.    YY:=0;
  156.  
  157.  
  158.    RWLoop:
  159.      For GG:=0+YY TO 23+YY DO
  160.        Begin
  161.          TextSeek(TxtFile,LineNumArray[GG]);
  162.          ReadLn(TxtFile,S);
  163.          MyText[GG-YY]:=S;
  164.        End;
  165.      GoToXY(1,1);
  166.      ColorSwitch := TextAttr;
  167.      Str(yy+23:5,LineNo);
  168.  
  169.      Repeat Until Port[$3DA] And 8 = 8; { Wait For Vertical retrace }
  170.  
  171.      For GG:=0 TO 23 DO
  172.        Begin
  173.          ClrEOL;
  174.          WriteLn(MyText[GG]);
  175.        End;
  176.      GoToXY(2,25);
  177.      TextColor(14);
  178.      Write(LineNo);
  179.      GoToXY(8,25);
  180.      TextColor(15);
  181.      Write(InstructStr);
  182.      TextAttr:=ColorSwitch;
  183.  
  184.      Delay(1);
  185.    WrongKey:
  186.      Repeat
  187.      Until KeyPressed;
  188.      Cee := ReadKey;
  189.  
  190.      If Cee=Chr(27) Then GoTo Final
  191.      Else If Cee=Chr(72) Then   {UP ARROW}
  192.        Begin
  193.          If YY>0 Then Dec(YY,1);
  194.          GoTo RWLoop;
  195.        End
  196.      Else If Cee=Chr(80) Then  {DOWN ARROW}
  197.        Begin
  198.          Inc(YY,1);
  199.          If YY>=Counter-23 Then YY:= Counter-24;
  200.          GoTo RWLoop;
  201.        End
  202.      Else If Cee=Chr(73) Then {PAGE UP}
  203.        Begin
  204.          YY:=YY-24;
  205.          If YY<1 Then YY:=0;
  206.          GoTo RWLoop;
  207.        End
  208.      Else If Cee=Chr(81) Then {PAGEDOWN}
  209.        Begin
  210.          YY:= YY+24;
  211.          If YY>=Counter-23 Then YY:= Counter-24;
  212.          GoTo RWLoop;
  213.        End
  214.      Else If Cee=Chr(71) Then  {HOME}
  215.        Begin
  216.          YY:=0;
  217.          GoTo RWLoop;
  218.        End
  219.      Else If Cee=Chr(79) Then  {End}
  220.        Begin
  221.          YY:= Counter-24;
  222.          GoTo RWLoop;
  223.        End;
  224.  
  225.    GoTo WrongKey;
  226.  
  227.   FileSizeError:
  228.     WriteLn;
  229.     WriteLn('ERROR...');
  230.     WriteLn;
  231.     WriteLn('File Size Larger Than 750,000');
  232.     GoTo Final;
  233.  
  234.   NoParamError:
  235.     WriteLn;
  236.     WriteLn('ERROR...');
  237.     WriteLn;
  238.     WriteLn('Command line syntax is Reader C:\TextFile.txt');
  239.     GoTo Final;
  240.  
  241.   Final:
  242.     Close(TxtFile);
  243.     TextAttr := OrigColor;
  244.     RestoreCursor;
  245.     ClrScr;
  246.  End.
  247.